define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    // Form.events.switcher($("from"));



    var Controller = {
        index: function () {
            // $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "日期";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'task_result_analyze/analyzeTable' + location.search,
                    table: 'table',
                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pageSize: 20, //调整分页大小为20
                pageList: [10, 25, 50, 100, 'All'],
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                fixedColumns:true,
                fixedNumber:2,
                columns: [
                    [
                        {field: 'id',
                            formatter:function (value,row,index) {
                                return index+1
                            }
                        },
                        {field: 'date',operate: 'RANGE', title: __('日期'), addclass:'datetimerange'},
                        {field: 'exposure', title: __('曝光量'),sortable:true,searchable:false},
                        {field: 'exposure_succ', title: __('曝光成功量'),searchable:false },
                        {
                            field: 'exposure_fail',
                            title: __('曝光失败量'),
                            searchable:false,
                            formatter:function (value,row) {
                                if(row.exposure_fail == 0){
                                    return 0;
                                }else{
                                    return row.exposure_fail+'&nbsp;&nbsp;&nbsp;<i class="fa fa-question-circle-o" onmouseenter="roleupdate(this)" onmouseleave="roleupdate2(this)" data-id="'+ row.id+'" data-flag="exposure" data-fail=\''+row.directions+'\' ></i>';
                                }
                            }
                        },
                        {field: 'task', title: __('执行任务量'),sortable:true,searchable:false},
                        {field: 'task_succ', title: __('执行任务成功量'),searchable:false},
                        {
                            field: 'task_fail',
                            title: __('执行任务失败量'),
                            searchable:false,
                            formatter:function (value,row) {
                                if(row.task_fail == 0){
                                    return 0;
                                }else{
                                    return row.task_fail+'&nbsp;&nbsp;&nbsp;<i class="fa fa-question-circle-o" onmouseenter="roleupdate(this)" onmouseleave="roleupdate2(this)" data-id="'+ row.id+'" data-flag="task" data-fail=\''+row.directions+'\' ></i>';
                                }
                            }
                        },
                        {field: 'task_find', title: __('查找节点量'),sortable:true,searchable:false},
                        {field: 'task_find_succ', title: __('查找节点成功量'),searchable:false},
                        {
                            field: 'task_find_fail',
                            title: __('查找节点失败量'),
                            searchable:false,
                            formatter:function (value,row) {
                                if(row.task_find_fail == 0){
                                    return 0;
                                }else{
                                    return row.task_find_fail+'&nbsp;&nbsp;&nbsp;<i class="fa fa-question-circle-o" onmouseenter="roleupdate(this)" onmouseleave="roleupdate2(this)" data-id="'+ row.id+'" data-flag="task_find" data-fail=\''+row.directions+'\' ></i>';
                                }
                            }
                        },
                        {field: 'task_click', title: __('点击节点量'),sortable:true,searchable:false},
                        {field: 'task_click_succ', title: __('点击节点成功量'),searchable:false},
                        {
                            field: 'task_click_fail',
                            title: __('点击节点失败量'),
                            searchable:false,
                            formatter:function (value,row) {
                                if(row.task_click_fail == 0){
                                    return 0;
                                }else{
                                    return row.task_click_fail+'&nbsp;&nbsp;&nbsp;<i class="fa fa-question-circle-o" onmouseenter="roleupdate(this)" onmouseleave="roleupdate2(this)" data-id="'+ row.id+'" data-flag="task_click" data-fail=\''+row.directions+'\' ></i>';
                                }
                            }
                        },
                        {field: 'task_load', title: __('加载页面量'),sortable:true,searchable:false},
                        {field: 'task_load_succ', title: __('加载页面成功量'),searchable:false},
                        {
                            field: 'task_load_fail',
                            title: __('加载页面失败量'),
                            searchable:false,
                            formatter:function (value,row) {
                                if(row.task_load_fail == 0){
                                    return 0;
                                }else{
                                    return row.task_load_fail+'&nbsp;&nbsp;&nbsp;<i class="fa fa-question-circle-o" onmouseenter="roleupdate(this)" onmouseleave="roleupdate2(this)" data-id="'+ row.id+'" data-flag="task_load" data-fail=\''+row.directions+'\' ></i>';
                                }
                            }
                        },
                        {field: 'updated_at', title: __('统计时间'),searchable:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function roleupdate(e){
    $(e).addClass('fa-question-circle');
    $(e).removeClass('fa-question-circle-o');
    var fa_this = $(e);
    var flag = $(e).attr('data-flag');
    var fail = $(e).attr('data-fail');

    var obj = eval('(' + fail + ')');
    console.log(obj);
    var data = obj[flag]
    var str2 = '';
    $.each(data.fail, function (k2, v2) {
        str2 += k2+'-------------------'+v2+'<br>';
    });
    layer.tips(
        "<span style='color:black'>"+str2+"</span>",
        fa_this,
        {
            area: ['300px', 'auto'],
            // area: 'auto',
            tips: [2, '#fff'],
            time: 30000
        }
    );

    return false;

}

function roleupdate2(e){
    $(e).addClass('fa-question-circle-o');
    $(e).removeClass('fa-question-circle');
    layer.closeAll();
}
